<?php

class widgetTask extends sfBaseTask
{
  protected function configure()
  {

    $this->addOptions(array(
      new sfCommandOption('name', null, sfCommandOption::PARAMETER_REQUIRED, 'The widget name'),
      new sfCommandOption('description', null, sfCommandOption::PARAMETER_REQUIRED, 'The description'),
      new sfCommandOption('section', null, sfCommandOption::PARAMETER_REQUIRED, 'The section where can be installed')
    ));

    $this->namespace        = 'wixet';
    $this->name             = 'generate-widget';
    $this->briefDescription = 'Generates a new widget';
    
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    
    $sections = Doctrine::getTable('Section');
	$sections = $sections->findByDql('name = ?', $options['section']);
	$section = $sections->getFirst();

    $widget = new Widget();
    $widget->setName($options['name']);
    $widget->setDescription($options['description']);
    if(is_object($section))
		$widget->setSection($section->getId());
	else
		echo 'Section "'.$options['section'].'" does not exist';

    $widget->save();
    $this->runTask('generate:module', array('ui',$options['name']));
    $this->log("Widget created");
  }
}
